package com.xmkj.bearuser.home;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;

import com.common.adapter.helper.IRecyclerViewHelper;
import com.common.mvp.BaseMvpActivity;
import com.common.mvp.BasePresenter;
import com.common.retrofit.bean.HomeListNewBean;
import com.common.retrofit.bean.HomeReccommedBean;
import com.common.retrofit.bearusermethod.GetNewListMethods;
import com.common.retrofit.bearusermethod.GetReccommendListMethods;
import com.common.retrofit.subscriber.CommonSubscriber;
import com.common.retrofit.subscriber.SubscriberListener;
import com.common.utils.EmptyUtils;
import com.common.widget.recyclerview.refresh.recycleview.XRecyclerView;
import com.xmkj.bearuser.R;
import com.xmkj.bearuser.home.user.HomeReccommendListAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Create by leo
 * 创建日期：2018/7/6
 * 描述：
 */

public class HomeButtomThreeActivity extends BaseMvpActivity {
    public static final String TYPE = "type";
    @BindView(R.id.recyclerview)
    XRecyclerView recyclerview;
    private String type;
    private HomeReccommendListAdapter homeReccommendListAdapter;
    List<HomeReccommedBean> homeReccommedBeans = new ArrayList<>();
    List<HomeListNewBean> homeListNewBeans = new ArrayList<>();
    private HomeNewListAdapter homeNewListAdapter;
    private HomeLoveListAdapter homeLoveListAdapter;

    @Override
    protected BasePresenter createPresenterInstance() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_base_reclerview;
    }

    @Override
    protected void onViewCreated() {

    }

    @Override
    protected void doLogicFunc() {
        switch (type) {
            case "1":  // 设计是推荐
                setRecommendRecyclerView();
                gotoRecommendListHttp();
                break;
            case "2": // 新款到店
                setNewRecyclerView();
                gotoNewHttp();
                break;
            case "3":  // 会员最爱
                setLikeRecyclerView();
                gotoLoveHttp();
                break;
        }
    }

    private void gotoLoveHttp() {
        CommonSubscriber<ArrayList<HomeListNewBean>> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                dismissProgressDialog();
                ArrayList<HomeListNewBean> beans = (ArrayList<HomeListNewBean>) o;
                List<HomeListNewBean> beanList = beans;
                recyclerview.loadMoreComplete();
                // 下拉刷新
                if (mIsRefreshOrLoadMore == 0) {
                    recyclerview.refreshComplete();
                    homeLoveListAdapter.clear();
                }

                if (EmptyUtils.isNotEmpty(beanList)) {
                    homeListNewBeans = beanList;
                    homeLoveListAdapter.addAll(homeListNewBeans);
                } else if (mIsRefreshOrLoadMore == 0) {
//                    statusEmpty();
                    return;
                }

                if (EmptyUtils.isEmpty(beanList)) {
                    recyclerview.setNoMore(true);
                } else {
                    mIsHasNoData = beanList.size() < mPageSize;
                    recyclerview.setNoMore(mIsHasNoData);
                }
                homeLoveListAdapter.notifyDataSetChanged();
                recyclerview.refreshComplete();
                recyclerview.loadMoreComplete();
                if (mIsRefreshOrLoadMore == 0) {
                    dismissProgressDialog();
                }
            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                homeLoveListAdapter.notifyDataSetChanged();
                recyclerview.refreshComplete();
                recyclerview.loadMoreComplete();
                if (mIsRefreshOrLoadMore == 0) {
                    dismissProgressDialog();
                }
            }
        });


        GetNewListMethods.getInstance().moreList(subscriber, 2, mPageIndex);
        rxManager.add(subscriber);
    }

    private void setLikeRecyclerView() {
        GridLayoutManager layoutManager = new GridLayoutManager(context, 2);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerview.setLayoutManager(layoutManager);
//        recyclerview.addItemDecoration(new DividerGridItemDecoration(context));
      /*  recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.VERTICAL, (int) SizeUtils.dp2px(context, 1)));
        recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.HORIZONTAL, (int) SizeUtils.dp2px(context, 1)));*/
//        recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.VERTICAL, (int) SizeUtils.dp2px(context, 1)));
        recyclerview.setHasFixedSize(true);
        recyclerview.setRefreshing(true);
        recyclerview.setLoadMoreEnabled(true);
        homeLoveListAdapter = new HomeLoveListAdapter(context, homeListNewBeans);
//        adapter.setOnItemClickSeclect(this);
        recyclerview.setAdapter(homeLoveListAdapter);
        recyclerview.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mPageIndex = 1;
                mIsRefreshOrLoadMore = 0;
                gotoLoveHttp();
            }

            @Override
            public void onLoadMore() {
                if (mIsHasNoData) {
                    recyclerview.loadMoreComplete();
                    recyclerview.setNoMore(true);
                    return;
                }
                mPageIndex++;
                mIsRefreshOrLoadMore = 1;
                gotoLoveHttp();
            }
        });
    }

    private void gotoNewHttp() {
        CommonSubscriber<ArrayList<HomeListNewBean>> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                dismissProgressDialog();
                ArrayList<HomeListNewBean> beans = (ArrayList<HomeListNewBean>) o;
                List<HomeListNewBean> beanList = beans;
                recyclerview.loadMoreComplete();
                // 下拉刷新
                if (mIsRefreshOrLoadMore == 0) {
                    recyclerview.refreshComplete();
                    homeNewListAdapter.clear();
                }

                if (EmptyUtils.isNotEmpty(beanList)) {
                    homeListNewBeans = beanList;
                    homeNewListAdapter.addAll(homeListNewBeans);
                } else if (mIsRefreshOrLoadMore == 0) {
//                    statusEmpty();
                    return;
                }

                if (EmptyUtils.isEmpty(beanList)) {
                    recyclerview.setNoMore(true);
                } else {
                    mIsHasNoData = beanList.size() < mPageSize;
                    recyclerview.setNoMore(mIsHasNoData);
                }
                homeNewListAdapter.notifyDataSetChanged();
                recyclerview.refreshComplete();
                recyclerview.loadMoreComplete();
                if (mIsRefreshOrLoadMore == 0) {
                    dismissProgressDialog();
                }
            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                homeNewListAdapter.notifyDataSetChanged();
                recyclerview.refreshComplete();
                recyclerview.loadMoreComplete();
                if (mIsRefreshOrLoadMore == 0) {
                    dismissProgressDialog();
                }
            }
        });


        GetNewListMethods.getInstance().moreList(subscriber, 1, mPageIndex);
        rxManager.add(subscriber);
    }

    private void setNewRecyclerView() {
        GridLayoutManager layoutManager = new GridLayoutManager(context, 2);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerview.setLayoutManager(layoutManager);
        recyclerview.setRefreshing(true);
        recyclerview.setLoadMoreEnabled(true);
//        recyclerview.addItemDecoration(new DividerGridItemDecoration(context));
      /*  recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.VERTICAL, (int) SizeUtils.dp2px(context, 1)));
        recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.HORIZONTAL, (int) SizeUtils.dp2px(context, 1)));*/
//        recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.VERTICAL, (int) SizeUtils.dp2px(context, 1)));
        recyclerview.setHasFixedSize(true);

        homeNewListAdapter = new HomeNewListAdapter(context, homeListNewBeans);
//        adapter.setOnItemClickSeclect(this);
        recyclerview.setAdapter(homeNewListAdapter);
        recyclerview.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mPageIndex = 1;
                mIsRefreshOrLoadMore = 0;
                gotoNewHttp();
            }

            @Override
            public void onLoadMore() {
                if (mIsHasNoData) {
                    recyclerview.loadMoreComplete();
                    recyclerview.setNoMore(true);
                    return;
                }
                mPageIndex++;
                mIsRefreshOrLoadMore = 1;
                gotoNewHttp();
            }
        });
    }

    private void gotoRecommendListHttp() {
        CommonSubscriber<ArrayList<HomeReccommedBean>> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                dismissProgressDialog();
                ArrayList<HomeReccommedBean> beans = (ArrayList<HomeReccommedBean>) o;
                List<HomeReccommedBean> beanList = beans;
                recyclerview.loadMoreComplete();
                // 下拉刷新
                if (mIsRefreshOrLoadMore == 0) {
                    recyclerview.refreshComplete();
                    homeReccommendListAdapter.clear();
                }

                if (EmptyUtils.isNotEmpty(beanList)) {
                    homeReccommedBeans = beanList;
                    homeReccommendListAdapter.addAll(homeReccommedBeans);
                } else if (mIsRefreshOrLoadMore == 0) {
//                    statusEmpty();
                    return;
                }

                if (EmptyUtils.isEmpty(beanList)) {
                    recyclerview.setNoMore(true);
                } else {
                    mIsHasNoData = beanList.size() < mPageSize;
                    recyclerview.setNoMore(mIsHasNoData);
                }
                homeReccommendListAdapter.notifyDataSetChanged();
                recyclerview.refreshComplete();
                recyclerview.loadMoreComplete();
                if (mIsRefreshOrLoadMore == 0) {
                    dismissProgressDialog();
                }
            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                homeReccommendListAdapter.notifyDataSetChanged();
                recyclerview.refreshComplete();
                recyclerview.loadMoreComplete();
                if (mIsRefreshOrLoadMore == 0) {
                    dismissProgressDialog();
                }
            }
        });


        GetReccommendListMethods.getInstance().recommendList(subscriber, mPageIndex);
        rxManager.add(subscriber);
    }

    @Override
    protected void setNavigation() {
        type = getIntent().getStringExtra(TYPE);
        switch (type) {
            case "1":
                setNavigationBack("设计师推荐");

                break;
            case "2":
                setNavigationBack("新款到店");

                break;
            case "3":
                setNavigationBack("会员最爱");

                break;

        }

    }

    @Override
    public void bindKnife() {
        ButterKnife.bind(this);
    }

    private void setRecommendRecyclerView() {
        IRecyclerViewHelper.init().setRecycleLineLayout(context, LinearLayoutManager.VERTICAL, recyclerview);

//        recyclerview.addItemDecoration(new DividerGridItemDecoration(context));
      /*  recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.VERTICAL, (int) SizeUtils.dp2px(context, 1)));
        recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.HORIZONTAL, (int) SizeUtils.dp2px(context, 1)));*/
//        recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.VERTICAL, (int) SizeUtils.dp2px(context, 1)));
        recyclerview.setHasFixedSize(true);
        recyclerview.setRefreshing(true);
        recyclerview.setLoadMoreEnabled(true);
        homeReccommendListAdapter = new HomeReccommendListAdapter(context, homeReccommedBeans);
//        adapter.setOnItemClickSeclect(this);
        recyclerview.setAdapter(homeReccommendListAdapter);

        recyclerview.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mPageIndex = 1;
                mIsRefreshOrLoadMore = 0;
                gotoRecommendListHttp();
            }

            @Override
            public void onLoadMore() {
                if (mIsHasNoData) {
                    recyclerview.loadMoreComplete();
                    recyclerview.setNoMore(true);
                    return;
                }
                mPageIndex++;
                mIsRefreshOrLoadMore = 1;
                gotoRecommendListHttp();
            }
        });

    }
}
